package start.demo01.datatype.json.entity.editor.stringEditor;

import org.apache.commons.lang3.StringUtils;
import start.demo01.datatype.json.entity.editor.Keywords;
import start.demo01.datatype.json.interfaces.StringEditor;
import start.demo01.datatype.json.pool.CacheMap;

//字符串截取
public class Truncation extends StringEditor {

    private Keywords start;
    private Keywords end;
    private String var;
    private final static String MATCH = "MATCH";

    //truncation(var,message,-1,end,0)
    //truncation(var,message,-1,MATCH)
    //truncation(var,message,-1)
    public Truncation(String name,String cmd) {
        super(name,cmd);
        String[] split = args.split(",");
        var = split[0];
        start = new Keywords(split[1], Integer.parseInt(split[2]));
        if (split.length == 4)
            end = new Keywords(split[3]);
        else if (split.length > 4)
            end = new Keywords(split[3], Integer.parseInt(split[4]));
    }

    @Override
    public void editor() {
        String origin = (String) map.get(name);
        int c;
        if (StringUtils.isEmpty(start.getKey()))
            c = start.getIndex();
        else
            c = origin.indexOf(start.getKey()) + start.getIndex();
        int d;
        if (end != null) {
            if (MATCH.equals(end.getKey())) {
                d = match(origin, start.getKey());
            } else {
                if (StringUtils.isEmpty(end.getKey()))
                    d = end.getIndex() + 1;
                else
                    d = origin.indexOf(end.getKey()) + end.getIndex() + 1;
            }
        } else
            d = origin.length();
        origin = origin.substring(c, d);
        map.put(var, origin);
    }

    private int match(String origin, String prefix) {
        int d = 0;
        //待开发
        return d;
    }
}
